package com.example.controller;


import com.example.entity.Lecture;
import com.example.entity.Teacher;
import com.example.service.UserService;
import com.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/test")
    public R<String> test(){
        return R.success("成功");

    }

    @PostMapping("/register")
    public R<String> teacherRegister(@RequestBody Teacher teacher){
        Teacher register = userService.teacherRegister(teacher);
        if (!Objects.isNull(register)){
            return R.error("工号已被注册");
        }

        return R.success("注册成功");

    }

    /**
     * 教师登录
     * @param teacher
     * @return
     */
    @PostMapping("/login")
    public R<Teacher> teacherLogin(@RequestBody Teacher teacher){
        Teacher teacherInfo = userService.teacherLogin(teacher);
        if (Objects.isNull(teacherInfo)){
            return R.error("账号或密码错误");
        }

        return R.success(teacherInfo);

    }



    /**
     * 教室生成班级信息并导入学生信息
     * @param className
     * @param classPosition
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/generate/class")
    public R<Map<String,Object>> generateClass(@RequestParam(value = "className") String className,
                                               @RequestParam(value = "classPosition") String classPosition,
                                               @RequestParam(value = "classTeacher") String classTeacher,
                                               @RequestParam(value = "file") MultipartFile file) throws Exception {
       return R.success(userService.generateClass(className,classPosition,classTeacher,file));

    }

    //导入学生信息表格
    @PostMapping("/importExcel")
    public R<Map<String,Object>> importExcel(@RequestParam(value = "file") MultipartFile file) throws Exception {
        Map<String, Object> map = userService.importExcel(file);
        return R.success(map);

    }

    /**
     * 页面初始化时载入所有课程信息
     * @return
     */
    @GetMapping("/lecture/list")
    public R<List> getLectureList(){
        return R.success(userService.selectLectureList());

    }

    /**
     * 依据班级id获取该班级的所有学生
     * @param lecture
     * @return
     */
    @CrossOrigin
    @PostMapping("/lecture/student")
    @ResponseBody
    public R<List> getLectureStudent(@RequestBody Lecture lecture){
//        return R.success(userService.getLectureStudent(lecture.getId()));
        return null;

    }

    @GetMapping("/student/list")
    public R<Map<String,Object>> getStudentList(@RequestParam(value = "id") String id){
        return R.success(userService.getLectureStudent(Long.valueOf(id)));

    }



}
